import org.bukkit.command.CommandExecutor;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import rdp.RDPMain;
import rdp.CMPMain;
import rdp.RDPUtil;
import rdp.auto.clear.AUTOClearMain;
import rdp.auto.notice.AUTONoticeMain;

import java.io.*;

public class Main extends JavaPlugin implements Listener {

    @Override
    public void onEnable() {
        saveDefaultConfig();
        FileConfiguration config = getConfig();
        saveConfig();

            CommandExecutor ce = new RDPMain(this,config);

            this.getCommand("r").setExecutor(ce);
            this.getCommand("rdp").setExecutor(ce);
            this.getCommand("rdp").setTabCompleter(new CMPMain());

        RDPUtil.LOG("info", "插件初始化成功，开始加载配置。");
        try {
            if (config.getBoolean("clear.enable")) {
                new AUTOClearMain().open(YamlConfiguration.loadConfiguration(getRDPConfig(config, "clear")), this);
            }
            if (config.getBoolean("notice.enable")) {
                new AUTONoticeMain().open(YamlConfiguration.loadConfiguration(getRDPConfig(config, "notice")), this);
            }
            RDPUtil.LOG("info", "配置加载成功。");
        } catch (IOException e) {
            RDPUtil.LOG("error", "加载失败。");
        }
        RDPUtil.SEND(new String[]{
                "§e+--------------------------------------------------+",
                "§e|§2================§a[RedenDetailPlugin]§2===============§e|",
                "§e|§2==============§aVersion-0.0.5-SNAPSHOT§2==============§e|",
                "§e|§2===================§aAuthor-Reden§2===================§e|",
                "§e+--------------------------------------------------+"
        });
        RDPUtil.LOG("info", "RDP插件已开启。");
    }

    @Override
    public void onDisable() {
        RDPUtil.LOG("error", "RDP插件已关闭。");
    }

    @Override
    public void onLoad() {

    }

    public File getRDPConfig(FileConfiguration config, String name) throws IOException {
        RDPUtil.LOG("warning", name + "功能已启用，开始加载 " + name + ".yml 。");
        File conf = new File(getDataFolder(), name + ".yml");
        if (!conf.exists() || config.getBoolean(name + ".reload")) {
            if (conf.exists())
                RDPUtil.LOG("warning", "重载已启用，开始重载。");
            else
                RDPUtil.LOG("warning", "文件 " + name + ".yml 不存在，开始重载。");
            setRDPConfig(conf, name);
            RDPUtil.LOG("info", "重载 " + name + ".yml 完毕。");
        }
        RDPUtil.LOG("info", "加载 " + name + ".yml 成功。");
        return conf;
    }

    public void setRDPConfig(File conf, String name) throws IOException {
        InputStream in = this.getClass().getResourceAsStream(name + ".yml");
        OutputStream out = new FileOutputStream(conf);
        byte[] bytes = new byte[1];
        while (in.read(bytes) != -1)
            out.write(bytes);
        out.flush();
    }
}
